package quq.missq.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by dabing on 2015/10/8.
 */
public class HttpHelper {
    private static String tag = HttpHelper.class.getSimpleName();

    /**
     * 执行http get请求
     * @param url
     * @return
     */
    public static String get(String url) {
        String result = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
//		TLog.log(tag, "请求的url: " + url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 请求成功
                HttpEntity entity = httpResponse.getEntity();// 获取响应体
                InputStream is = entity.getContent();// 获取流对象
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];// 1k的缓冲区
                int len = -1;// 记录每次读取到的字节长度
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                    baos.flush();// 保存缓冲区的数据刷到输出流中
                }
                // 关闭流和链接
                is.close();
                // baos.close();//然而这个并不需要关
                httpClient.getConnectionManager().closeExpiredConnections();// 关闭闲置链接

                result = new String(baos.toByteArray());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//		TLog.log(tag, "响应的数据: " + result);
        return result;
    }

    /**
     * 执行http get请求获取输入流，使用者应该使用完毕流后关闭该流
     * @param url
     * @return
     */
    public static InputStream getInputStream(String url) {
        InputStream result = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 请求成功
                HttpEntity entity = httpResponse.getEntity();// 获取响应体
                result = entity.getContent();// 获取流对象
                // httpClient.getConnectionManager().closeExpiredConnections();//关闭闲置链接
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 关闭指定输入流
     * @param is
     */
    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭指定输出流
     * @param os
     */
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
